package com.example.sketchpaddemo;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;


import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.support.annotation.Nullable;
import android.util.AttributeSet;


import android.view.MotionEvent;
import android.view.View;

public class Sketchpad extends View {
    private Paint paint;
    private Canvas bufferCanvas;
    private Bitmap bufferBitmap;


    public static final int RECT = 1;
    public static final int PEN = 0;
    public static final int CIRCLE = 2;
    public static final int ERASER = 3;


    Path path = new Path();
    private int paintMode = 0;

    private Point rectLeftTop = new Point();
    private Point rectRightBottom = new Point();

    private Point circleCenter = new Point();
    private float radius = 0;



    public Sketchpad(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setDither(true);
        StratThread();
    }

    public void setPaintColor(int color){
        paint.setColor(color);
    }

    /**
     * StratThread:启动线程每0.3秒向onDraw刷新一次
     */
    private void StratThread() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    postInvalidate();
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        bufferBitmap = Bitmap.createBitmap(right,bottom,Bitmap.Config.ARGB_8888);
        bufferCanvas = new Canvas(bufferBitmap);

    }

  /*  public void setCurrencenter(float currencenter) {
        this.currencenter = currencenter;
        drawSth();
    }*/

    private void drawSth() {
        if(bufferBitmap== null)return;



        /*bufferCanvas.drawColor(Color.WHITE);
        bufferCanvas.drawCircle(currencenter,currencenter,200,paint);*/
        switch (paintMode){
            case PEN:
                bufferCanvas.drawPath(path, paint);
                break;

            case RECT:

                bufferCanvas.drawRect(rectLeftTop.x, rectLeftTop.y, rectRightBottom.x, rectRightBottom.y, paint);
                break;

            case CIRCLE:
                bufferCanvas.drawCircle(circleCenter.x, circleCenter.y, radius, paint);
                break;

            /*case ERASER:
                bufferCanvas.drawPath(path,paint);
                break;*/
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bufferBitmap,0,0,null);

    }

    public void setPaintMode(int paintMode) {
        this.paintMode = paintMode;
        drawSth();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (paintMode){
            case PEN:

            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    path.moveTo(event.getX(),event.getY());
                    break;

                    case MotionEvent.ACTION_MOVE:
                        path.lineTo(event.getX(),event.getY());
                        invalidate();
                        break;

                        case MotionEvent.ACTION_UP:
                            break;
            }
                bufferCanvas.drawPath(path,paint);


            break;

            case RECT:

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        rectLeftTop.x = (int) event.getX();
                        rectLeftTop.y = (int) event.getY();
                        break;

                        case MotionEvent.ACTION_MOVE:
                            rectRightBottom.x = (int) event.getX();
                            rectRightBottom.y = (int) event.getY();
                            invalidate();
                            break;

                            case MotionEvent.ACTION_UP:
                                break;
                }
                bufferCanvas.drawColor(Color.WHITE);

                bufferCanvas.drawRect(rectLeftTop.x,rectLeftTop.y,rectRightBottom.x,rectRightBottom.y,paint);
                break;

            case CIRCLE:

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        circleCenter.x = (int) event.getX();
                        circleCenter.y = (int) event.getY();
                        break;

                        case MotionEvent.ACTION_MOVE:
                            radius = (float) Math.sqrt(Math.pow(event.getX()-circleCenter.x,2)+Math.pow(event.getY()-circleCenter.y,2));
                            invalidate();
                            break;

                            case MotionEvent.ACTION_UP:
                                break;
                }

                bufferCanvas.drawColor(Color.WHITE);
                bufferCanvas.drawCircle(circleCenter.x,circleCenter.y,radius,paint);

                break;

            /*case ERASER:
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        //清除路径的内容
                        path.reset();
                        break;
                }


                break;*/
        }






        return true;
    }

    public void clear(){
        bufferCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }



}
